/**
 * @file models/overview.js
 * @author
 */
import {routerRedux} from 'dva/router';
import api from '../api';
import {message} from 'antd'

export default {
  namespace : 'menu',
  state : {
    currentRoleCode:'ROLE_ADMIN',
    theRoleMenus:[],
    setupMenus:null,
    checkedKeys:null,
  },
  effects : {
    * getTheRoleMenus({
      payload
    }, {call, put, select}) {
      const theRoleMenus=(yield call(api.role.getTheRoleMenus,payload)).data
      yield put({
        type:'setState',
        payload:{
          theRoleMenus,
        }
      })
    },
    * setupTheRoleMenu({
      payload
    }, {call, put, select}) {
      const state=yield select(state=>state)
      const res=yield call(api.role.setupTheRoleMenu,payload)
      const roleName=state.role.roles.find(item=>item.roleCode===state.menu.currentRoleCode).roleName

      if(res.code===0){
        message.success(`${roleName}的菜单设置成功！`)
      }else{
        message.error('菜单设置失败！')
      }
    },
  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    },
    clearState(state) {
      return {
        ...state
      }
    },
  }
}
